package logicaBancoDeDados;

import classes.Conector;
import classes.Garantia;
import java.sql.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class ConectaGarantias {

    public Conector conect = new Conector();
    public PreparedStatement prest = null;
    public String sql = null;
    public ResultSet rs = null;
    public ArrayList<Garantia> listaGarantia = new ArrayList<Garantia>();

    public ConectaGarantias() {
    }

    public ArrayList<Garantia> listAll() {
        try {
            sql = "select * from garantias";
            prest = conect.conn.prepareStatement(sql);
            rs = prest.executeQuery();
            while (rs.next()) {
                Garantia g1 = new Garantia();
                g1.setId(rs.getInt("id"));
                g1.setDistribuidor(rs.getString("distribuidor"));
                g1.setVendedor(rs.getString("vendedor"));
                g1.setProduto(rs.getString("produto"));
                g1.setDataSaida(rs.getString("dataSaida"));
                g1.setDataRetorno(rs.getString("dataRetorno"));
                g1.setStatus(rs.getInt("status"));
                g1.setCodigoSaida(rs.getInt("codigoSaida"));
                listaGarantia.add(g1);
                prest.close();
                rs.close();
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        }
        return listaGarantia;
    }

    public void insere(Garantia garantia) {
        try {
            sql = "insert into garantias (produto,distribuidor,vendedor,dataSaida,dataRetorno,status,codigoSaida) VALUES(?,?,?,?,?,?,?)";
            prest = conect.conn.prepareStatement(sql);
            prest.setString(1, garantia.getProduto());
            prest.setString(2, garantia.getDistribuidor());
            prest.setString(3, garantia.getVendedor());
            prest.setString(4, garantia.getDataSaida());
            prest.setString(5, garantia.getDataRetorno());
            prest.setInt(6, garantia.getStatus());
            prest.setInt(7, garantia.getCodigoSaida());
            prest.executeQuery();
            prest.close();
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }

    public void remove(Garantia garantia) {
        try {
            sql = "delete from garantias where id = ?";
            prest.setInt(1, garantia.getId());
            prest.executeQuery();
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }

    public void altera(Garantia garantia) {
        try {
            sql = "UPDATE garantias set produto = ?, distribuidor = ?, vendedor = ?, dataSaida = ?, dataRetorno = ?, status = ?, codigoSaida = ? where id = ?";
            prest = conect.conn.prepareStatement(sql);
            prest.setString(1, garantia.getProduto());
            prest.setString(2, garantia.getDistribuidor());
            prest.setString(3, garantia.getVendedor());
            prest.setString(4, garantia.getDataSaida());
            prest.setString(5, garantia.getDataRetorno());
            prest.setInt(6, garantia.getStatus());
            prest.setInt(7, garantia.getCodigoSaida());
            prest.setInt(8, garantia.getId());
            prest.executeUpdate();
            prest.close();
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }

    public ArrayList<Garantia> listaStatus(int status) {
        try {
            sql = "select * from garantias where status = ?";
            prest.setInt(1, status);
            prest = conect.conn.prepareStatement(sql);
            rs = prest.executeQuery();
            while (rs.next()) {
                Garantia g1 = new Garantia();
                g1.setId(rs.getInt("id"));
                g1.setDistribuidor(rs.getString("distribuidor"));
                g1.setVendedor(rs.getString("vendedor"));
                g1.setProduto(rs.getString("produto"));
                g1.setDataSaida(rs.getString("dataSaida"));
                g1.setDataRetorno(rs.getString("dataRetorno"));
                g1.setStatus(rs.getInt("status"));
                listaGarantia.add(g1);
                prest.close();
                rs.close();
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        }
        return listaGarantia;
    }

    public boolean isCadastrada(Garantia garantia) {
        boolean cond = false;
        try {
            sql = "select * from garantias where produto = ? and codSaida = ?";
            prest = conect.conn.prepareStatement(sql);
            prest.setString(1, garantia.getProduto());
            prest.setInt(2, garantia.getCodigoSaida());
            rs = prest.executeQuery();
            if (rs.next()) {
                cond = true;
                if (garantia.getId() == rs.getInt("id") && garantia.getCodigoSaida() == rs.getInt("codigoSaida")) {
                    cond = false;
                }
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Erro ao consultar");
        }
        return cond;
    }
}
